'use strict';

const Controller = require('egg').Controller;

class SocialController extends Controller {
  // 分享搭配
  async shareOutfit() {
    const { ctx, service } = this;



    try {
      const { outfitId, platform, caption, isPublic = true } = ctx.request.body;
      
      if (!outfitId) {
        ctx.throw(400, '缺少搭配ID');
      }

      const share = await service.social.shareOutfit(
        ctx.state.user?.id || 1,
        { outfitId, platform, caption, isPublic }
      );

      ctx.body = {
        code: 200,
        message: '分享成功',
        data: share,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 获取公开搭配
  async getPublicOutfits() {
    const { ctx, service } = this;

    try {
      const { page = 1, pageSize = 20, style, season, sortBy = 'latest' } = ctx.query;
      
      const outfits = await service.social.getPublicOutfits({
        page: parseInt(page),
        pageSize: parseInt(pageSize),
        style,
        season,
        sortBy
      });

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: outfits,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 点赞搭配
  async likeOutfit() {
    const { ctx, service } = this;



    try {
      const { outfitId } = ctx.params;
      
      const result = await service.social.likeOutfit(
        ctx.state.user?.id || 1,
        outfitId
      );

      ctx.body = {
        code: 200,
        message: result.liked ? '点赞成功' : '取消点赞',
        data: result,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 评论搭配
  async commentOutfit() {
    const { ctx, service } = this;



    try {
      const { outfitId } = ctx.params;
      const { content, rating } = ctx.request.body;
      
      if (!content) {
        ctx.throw(400, '评论内容不能为空');
      }

      const comment = await service.social.commentOutfit(
        ctx.state.user?.id || 1,
        outfitId,
        { content, rating }
      );

      ctx.body = {
        code: 200,
        message: '评论成功',
        data: comment,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 获取搭配评论
  async getOutfitComments() {
    const { ctx, service } = this;

    try {
      const { outfitId } = ctx.params;
      const { page = 1, pageSize = 20 } = ctx.query;
      
      const comments = await service.social.getOutfitComments(outfitId, {
        page: parseInt(page),
        pageSize: parseInt(pageSize)
      });

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: comments,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 关注用户
  async followUser() {
    const { ctx, service } = this;



    try {
      const { userId } = ctx.params;
      
      if (userId == ctx.state.user?.id || 1) {
        ctx.throw(400, '不能关注自己');
      }

      const result = await service.social.followUser(
        ctx.state.user?.id || 1,
        userId
      );

      ctx.body = {
        code: 200,
        message: result.followed ? '关注成功' : '取消关注',
        data: result,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 获取关注列表
  async getFollowing() {
    const { ctx, service } = this;



    try {
      const { page = 1, pageSize = 20 } = ctx.query;
      
      const following = await service.social.getFollowing(ctx.state.user?.id || 1, {
        page: parseInt(page),
        pageSize: parseInt(pageSize)
      });

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: following,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 获取粉丝列表
  async getFollowers() {
    const { ctx, service } = this;



    try {
      const { page = 1, pageSize = 20 } = ctx.query;
      
      const followers = await service.social.getFollowers(ctx.state.user?.id || 1, {
        page: parseInt(page),
        pageSize: parseInt(pageSize)
      });

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: followers,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 获取好友推荐
  async getFriendRecommendations() {
    const { ctx, service } = this;



    try {
      const recommendations = await service.social.getFriendRecommendations(
        ctx.state.user?.id || 1
      );

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: recommendations,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 获取动态流
  async getFeed() {
    const { ctx, service } = this;



    try {
      const { page = 1, pageSize = 20 } = ctx.query;
      
      const feed = await service.social.getFeed(ctx.state.user?.id || 1, {
        page: parseInt(page),
        pageSize: parseInt(pageSize)
      });

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: feed,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 举报内容
  async reportContent() {
    const { ctx, service } = this;



    try {
      const { contentType, contentId, reason, description } = ctx.request.body;
      
      if (!contentType || !contentId || !reason) {
        ctx.throw(400, '缺少必要参数');
      }

      await service.social.reportContent(
        ctx.state.user?.id || 1,
        { contentType, contentId, reason, description }
      );

      ctx.body = {
        code: 200,
        message: '举报成功',
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }
}

module.exports = SocialController;
